package com.tudouji.project.pay.controller;

import com.tudouji.common.utils.poi.ExcelTempUtil;
import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.interceptor.annotation.RepeatSubmit;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.pay.domain.Payinfo;
import com.tudouji.project.pay.service.IPayinfoService;
import com.tudouji.project.system.domain.Tuser;
import com.tudouji.project.system.service.ITuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 支付Controller
 *
 * @author tudouji
 * @date 2021-04-06
 */
@RestController
@RequestMapping("/pay/payinfo")
public class PayinfoController extends BaseController
{
    @Autowired
    private IPayinfoService payinfoService;
    @Autowired
    private ITuserService tuserService;
    @Autowired
    private ExcelTempUtil excelTempUtil;

    /**
     * 查询支付列表-老的流程19
     */
    @GetMapping("/list")
    public TableDataInfo list(Payinfo payinfo)
    {
        startPage();
        payinfo.setFlowid("19");
        List<Payinfo> list = payinfoService.selectPayinfoListOld(payinfo);
        return getDataTable(list);
    }

    /**
     * 查询支付列表-货运单支付
     */
    @GetMapping("/listForFreightorder")
    public TableDataInfo listForFreightorder(Payinfo payinfo)
    {
        startPage();
        List<Payinfo> list = payinfoService.selectPayinfoListFo(payinfo);
        return getDataTable(list);
    }

    /**
     * 查询支付列表-货运单支付-导出
     */
    @GetMapping("/listForFreightorderExport")
    public AjaxResult listForFreightorderExport(Payinfo payinfo)
    {
        List<Payinfo> list = payinfoService.selectPayinfoListFo(payinfo);
//        ExcelUtil<Payinfo> util = new ExcelUtil<Payinfo>(Payinfo.class);
//        return util.exportExcel(list, "payinfo");
        return excelTempUtil.exportCommon(list, "采收付款申请");
    }

    /**
     * 查询支付列表-老采购单支付
     */
    @GetMapping("/listForPurchasebill")
    public TableDataInfo listForPurchasebill(Payinfo payinfo)
    {
        startPage();
        List<Payinfo> list = payinfoService.selectPayinfoListFl(payinfo);
        return getDataTable(list);
    }
    /**
     * 查询支付列表-老采购单支付-导出
     */
    @GetMapping("/listForPurchasebillExport")
    public AjaxResult listForPurchasebillExport(Payinfo payinfo)
    {
        List<Payinfo> list = payinfoService.selectPayinfoListFl(payinfo);
//        ExcelUtil<Payinfo> util = new ExcelUtil<Payinfo>(Payinfo.class);
//        return util.exportExcel(list, "payinfo");
        return excelTempUtil.exportCommon(list, "采购付款申请");
    }
    /**
     * 查询支付列表-微信
     */
    @GetMapping("/listWx")
    public TableDataInfo listWx(Payinfo payinfo, String status, String functionType)
    {
        List<Payinfo> list = payinfoService.listWx(payinfo, status, functionType);
        return getDataTable(list);
    }

    /**
     * 获取种植户（乙方）
     */
    @GetMapping("/getPlanters")
    public AjaxResult getPlanters() {
        List<Tuser> planters = new ArrayList<Tuser>();
        try {
            Tuser tuser = new Tuser();
            tuser.setUsertype(1L);
            planters = tuserService.selectTuserList(tuser);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            return  AjaxResult.success(planters);
        }
    }

    /**
     * 导出支付列表
     */
    @PreAuthorize("@ss.hasPermi('pay:payinfo:export')")
    @Log(title = "支付", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Payinfo payinfo)
    {
        List<Payinfo> list = payinfoService.selectPayinfoList(payinfo);
        ExcelUtil<Payinfo> util = new ExcelUtil<Payinfo>(Payinfo.class);
        return util.exportExcel(list, "payinfo");
    }

    /**
     * 获取支付详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(payinfoService.selectPayinfoDetailById(id));
    }

    /**
     * 新增支付
     */
    @PreAuthorize("@ss.hasPermi('pay:payinfo:add')")
    @Log(title = "支付", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Payinfo payinfo)
    {
        return toAjax(payinfoService.insertPayinfo(payinfo));
    }

    /**
     * 修改支付
     */
    @PreAuthorize("@ss.hasPermi('pay:payinfo:edit')")
    @Log(title = "支付", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Payinfo payinfo)
    {
        return toAjax(payinfoService.updatePayinfo(payinfo));
    }

    /**
     * 删除支付
     */
    @PreAuthorize("@ss.hasPermi('pay:payinfo:remove')")
    @Log(title = "支付", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids)
    {
        return toAjax(payinfoService.deletePayinfoByIds(guids));
    }

    /**
     * 获取待申请货运单
     **/
 /*   @GetMapping("/getUnPayFreightorder")
    public AjaxResult getUnPayFreightorder() {
        return payinfoService.getUnPayFreightorder();
    }*/

    /**
     * 获取待申请货运单
     **/
    @GetMapping("/getUnPayFreightorderBySupplier")
    public AjaxResult getUnPayFreightorderBySupplier(@RequestParam(required = false) Long orgId,
                                                     @RequestParam(required = false) Long supplierId,
                                                     @RequestParam(required = false) String keywords) {
        return payinfoService.statisticsOrderByPlan(orgId, supplierId, keywords);
    }

    /**
     * 获取待申请货运单
     **/
    @GetMapping("/getUnPayFreightorders")
    public AjaxResult getUnPayFreightorders(@RequestParam(required = false) Long orgId,
                                                     @RequestParam(required = false) Long supplierId) {
        return payinfoService.getUnPayFreightorders(orgId, supplierId);
    }

    /**
     * 获取待申请采购单-汇总
     **/
    @GetMapping("/getUnPayPurchasebillGroup")
    public AjaxResult getUnPayPurchasebillGroup(@RequestParam(required = false) Long orgId,
                                                     @RequestParam(required = false) Long supplierId,
                                                @RequestParam(required = false) String keywords) {
        return payinfoService.getUnPayPurchasebillGroup(orgId, supplierId, keywords);
    }

    /**
     * 获取待申请采购单
     **/
    @GetMapping("/getUnPayPurchasebills")
    public AjaxResult getUnPayPurchasebills(@RequestParam(required = false) Long orgId,
                                                     @RequestParam(required = false) Long supplierId) {
        return payinfoService.getUnPayPurchasebills(orgId, supplierId);
    }

    /**
     * 发起
     **/
    @RepeatSubmit
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Payinfo payinfo, String fileIds, @RequestParam(required = false)Long taskid
            , @RequestParam(required = false)String opinion) {
        return payinfoService.save(payinfo, fileIds, taskid, opinion);
    }

    /**
     * 财务审批
     **/
    @RepeatSubmit
    @PostMapping("/finance")
    public AjaxResult finance(Long id, Long taskid, String opinion, String fileIds, Integer invoiceOn) {
        return payinfoService.finance(id, taskid, opinion, fileIds, invoiceOn);
    }

    /**
     * 支付
     **/
    @RepeatSubmit
    @PostMapping("/pay")
    public AjaxResult pay(Long id, Long taskid, String opinion, String fileIds, BigDecimal applyAmount, Integer continuepayFlag) {
        return payinfoService.pay(id, taskid, opinion, fileIds, applyAmount, continuepayFlag);
    }

    /**
     * 尾款支付
     **/
    @RepeatSubmit
    @PostMapping("/finalPay")
    public AjaxResult finalPay(Long id, Long taskid, String opinion, String fileIds, BigDecimal applyAmount) {
        return payinfoService.finalPay(id, taskid, opinion, fileIds, applyAmount);
    }

    /**
     * 审批
     **/
    @RepeatSubmit
    @PostMapping("/approval")
    public AjaxResult approval(Long id, Long taskid, String opinion, String fileIds) {
        return payinfoService.approval(id, taskid, opinion, fileIds);
    }

    @RepeatSubmit
    @Log(title = "拒绝", businessType = BusinessType.OTHER)
    @PostMapping("/refuse")
    public AjaxResult refuse(Long taskid, Long id, String opinion, String fileIds, Long refuseNodeid, Long backUserid) {
        return payinfoService.refuse(id, fileIds, opinion, taskid, refuseNodeid, backUserid);
    }

    /**
     * 微信--删除
     */
    @RepeatSubmit
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public AjaxResult del(Long id) {
        return payinfoService.del(id);
    }
}
